#include "paytypedialog.h"
#include "ui_paytypedialog.h"
#include <QInputDialog>
#include <QMessageBox>

PayTypeDialog::PayTypeDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PayTypeDialog)
{
    ui->setupUi(this);
    model = new QSqlQueryModel();
    model->setQuery(QString("select distinct name from pig_pay_types"));
    ui->listView->setModel(model);
    ui->listView->show();
}

PayTypeDialog::~PayTypeDialog()
{
    delete model;
    delete ui;
}

void PayTypeDialog::on_pushButton_2_clicked()
{
    QSqlQuery q;
    bool isOK;
    QString name = QInputDialog::getText(this, "New Payment Type", "Name", QLineEdit::Normal, "", &isOK);
    if (name.isEmpty() && isOK) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("The payment type name cannot be null!"));
        return;
    }
    if (name.length() > 255) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("Max length:255"));
        return;
    }
    q.prepare("select pay_type_id from pig_pay_types where name=:name");
    q.bindValue(":name", name);
    q.exec();
    if (!q.isActive()) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("System Error!"));
        return;
    }
    if (q.next()) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("The name you entered is already exists!"));
        return;
    }
    else {
        q.prepare("insert into pig_pay_types (name,description,last_modified_date) values (:name,:description,datetime('now'))");
        q.bindValue(":name", name);
        q.bindValue(":description", name);
        q.exec();
        model->setQuery(QString("select distinct name from pig_pay_types"));
    }

}

void PayTypeDialog::on_listView_doubleClicked(const QModelIndex &index)
{
    if (!index.isValid())
        return;
    bool isOK;
    QString oldname = ui->listView->currentIndex().data().toString();
    QString name = QInputDialog::getText(this, "Pig Bills", "Name", QLineEdit::Normal, oldname, &isOK);
    if (name.isEmpty() && isOK) {
        QMessageBox::warning(this, tr("Pig Budget"), tr("The payment type name cannot be null!"));
        return;
    }
    if (name.length() > 255) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("Max length:255"));
        return;
    }
    QSqlQuery q;
    q.prepare("select pay_type_id from pig_pay_types where name=:name");
    q.bindValue(":name", name);
    q.exec();
    if (!q.isActive()) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("System Error!"));
        return;
    }
    if (q.next()) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("The name you entered is already exists!"));
        return;
    }
    else {
        q.prepare("update pig_pay_types set name=:name, description=:description, last_modified_date=datetime('now') where name=:oldname");
        q.bindValue(":name", name);
        q.bindValue(":description", name);
        q.bindValue(":oldname", oldname);
        q.exec();
        model->setQuery(QString("select distinct name from pig_pay_types"));
    }
}

void PayTypeDialog::on_pushButton_clicked()
{
    on_listView_doubleClicked(ui->listView->currentIndex());
}
